// Plane.cpp
// 
/////////////////////////////////////////////////////////

#include "Plane.hpp"

Plane::Plane(Vec3f &v1, Vec3f &v2, Vec3f &v3) 
{
    SetPlanePoints(v1, v2, v3);
}


Plane::Plane() 
{
}

Plane::~Plane() 
{
}

void Plane::SetPlanePoints(Vec3f &v1, Vec3f &v2, Vec3f &v3) 
{
    Vec3f aux1, aux2;

    aux1 = v1 - v2;
    aux2 = v3 - v2;

    normal = aux2 * aux1;

    normal.normalize();
    point = Vec3f(v2);
    d = -(normal.dot(point));
}

void Plane::SetNormalAndPoint(Vec3f &normal, Vec3f &point) 
{

    this->normal = Vec3f(normal);
    this->normal.normalize();
    d = -(this->normal.dot(point));
}

void Plane::SetCoefficients(float a, float b, float c, float d) 
{
    // set the normal vector
    normal = Vec3f(a, b, c);
    //compute the lenght of the vector
    float l = normal.length();
    // normalize the vector
    normal = Vec3f(a/l, b/l, c/l);
    // and divide d by th length as well
    this->d = d/l;
}

float Plane::Distance(Vec3f &p) {

    return (d + normal.dot(p));
}
